/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jpamodel;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import util.converter.DateConverter;

/**
 *
 * @author Joseph
 */
@Entity
@Table(name = "CommissionWare")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "CommissionWare.findAll", query = "SELECT c FROM CommissionWare c"),
    @NamedQuery(name = "CommissionWare.findByItemID", query = "SELECT c FROM CommissionWare c WHERE c.itemID = :itemID"),
    @NamedQuery(name = "CommissionWare.findByName", query = "SELECT c FROM CommissionWare c WHERE c.name = :name"),
    @NamedQuery(name = "CommissionWare.findByDateIn", query = "SELECT c FROM CommissionWare c WHERE c.localDateIn = :localDateIn"),
    @NamedQuery(name = "CommissionWare.findByDateOut", query = "SELECT c FROM CommissionWare c WHERE c.localDateOut = :localDateOut")})
public class CommissionWare implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final double COMMISION = 0.20d;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "item_ID")
    private Integer itemID;
    @Column(name = "name")
    private String name;
    @Column(name = "dateIn")
    @Temporal(TemporalType.TIMESTAMP)
    private LocalDate localDateIn;
    @Column(name = "dateOut")
    @Temporal(TemporalType.TIMESTAMP)
    private LocalDate localDateOut;
    @Transient
    private Date dateIn;
    @Transient
    private Date dateOut;
    @JoinColumn(name = "fk_category", referencedColumnName = "cat_ID")
    @ManyToOne(optional = false)
    private Category category;
    @JoinColumn(name = "fk_seller", referencedColumnName = "phone")
    @ManyToOne(optional = false)
    private Seller seller;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "fkitemID")
    private List<PriceHistory> priceHistoryList;
    @Transient
    private int currentPrice;
    @Transient
    private int commission;

    public CommissionWare() {
        priceHistoryList = new ArrayList<>();
    }

    public CommissionWare(Integer itemID) {
        this.itemID = itemID;
    }

    public Integer getItemID() {
        return itemID;
    }

    public void setItemID(Integer itemID) {
        this.itemID = itemID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDateIn() {
        if (dateIn == null) {
            if (localDateIn == null) {
                this.dateIn = null;
            } else {
                this.dateIn = DateConverter.convertLocalDateToDate(localDateIn);
            }
        }
        return dateIn;
    }

    public void setDateIn(Date dateIn) {
        if (dateIn == null) {
            if (localDateIn == null) {
                this.dateIn = null;
            } else {
                this.dateIn = DateConverter.convertLocalDateToDate(localDateIn);
            }
        } else {
            this.dateIn = dateIn;
        }
        this.dateIn = dateIn;
    }

    public Date getDateOut() {
        if (dateOut == null) {
            if (localDateOut == null) {
                this.dateOut = null;
            } else {
                this.dateOut = DateConverter.convertLocalDateToDate(localDateOut);
            }
        }
        return dateOut;
    }

    public void setDateOut(Date dateOut) {
         if (dateOut == null) {
            if (localDateOut == null) {
                this.dateIn = null;
            } else {
                this.dateOut = DateConverter.convertLocalDateToDate(localDateIn);
            }
        } else {
            this.dateIn = dateOut;
        }
    }
    
    public LocalDate getLocalDateIn() {
        return localDateIn;
    }

    public void setLocalDateIn(LocalDate dateIn) {
        this.localDateIn = dateIn;
    }

    public LocalDate getLocalDateOut() {
        return localDateOut;
    }

    public void setLocalDateOut(LocalDate dateOut) {
        this.localDateOut = dateOut;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Seller getSeller() {
        return seller;
    }

    public void setSeller(Seller seller) {
        this.seller = seller;
        System.out.println(seller.getName());
    }

    @XmlTransient
    public List<PriceHistory> getPriceHistoryList() {
        return priceHistoryList;
    }

    public void setPriceHistoryList(List<PriceHistory> priceHistoryList) {
        this.priceHistoryList = priceHistoryList;
    }

    public int getCommission() {
        if (priceHistoryList == null || priceHistoryList.isEmpty()) {
            commission = 0;
        } else {
            double val = COMMISION * (double) (priceHistoryList.get(priceHistoryList.size() - 1).getPrice());
            commission = (int) val;
        }
        return commission;
    }

    public void setCommission(int value) {
        double val = COMMISION * (double) value;
        commission = (int) val;
    }

    public int getCurrentPrice() {
        if (priceHistoryList == null || priceHistoryList.isEmpty()) {
            currentPrice = 0;
        } else {
            currentPrice = priceHistoryList.get(priceHistoryList.size() - 1).getPrice();
        }
        return currentPrice;
    }

    public void setCurrentPrice(int price) {
        PriceHistory ph = new PriceHistory();
        ph.setDateOfChange(LocalDate.now());
        ph.setPrice(price);
        ph.setFkitemID(this);
        priceHistoryList.add(ph);
        currentPrice = ph.getPrice();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (itemID != null ? itemID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CommissionWare)) {
            return false;
        }
        CommissionWare other = (CommissionWare) object;
        return !((this.itemID == null && other.itemID != null) || (this.itemID != null && !this.itemID.equals(other.itemID)));
    }

    @Override
    public String toString() {
        return name;
    }

    public boolean isUnsold() {
        return localDateOut == null;
    }

}
